import Vue from 'vue'
import $web3js from '../web3.js';

const ifilJointContract = require('./ifilJointContract.json')

let web3 = $web3js.getWeb3();

let contractConst = {
  ifilJointAddress: ifilJointContract.address,
  ifilJointContract: web3 ? new web3.eth.Contract(ifilJointContract.abi, ifilJointContract.address) : '',
}

const getIfilJointAddress = () => {
  return contractConst.ifilJointAddress;
}

const getIfilJointContract = () => {
  return contractConst.ifilJointContract;
}

const jointUserDeposit = (_vendorid, _amount) => {
  _amount = Vue.prototype.$numberUtils.movePointRight(_amount, 18);
  return $web3js.commonSend(getIfilJointContract().methods.userDeposit, _vendorid, _amount);
}

const jointUserDepositEstimateGas = (_vendorid, _amount) => {
  _amount = Vue.prototype.$numberUtils.movePointRight(_amount, 18);
  return $web3js.commonEstimateGas(getIfilJointContract().methods.userDeposit, _vendorid, _amount);
}

const jointUserRedeem = (_vendorid, _amount) => {
  _amount = Vue.prototype.$numberUtils.movePointRight(_amount, 18);
  return $web3js.commonSend(getIfilJointContract().methods.userRedeem, _vendorid, _amount);
}

const jointUserRedeemEstimateGas = (_vendorid, _amount) => {
  _amount = Vue.prototype.$numberUtils.movePointRight(_amount, 18);
  return $web3js.commonEstimateGas(getIfilJointContract().methods.userRedeem, _vendorid, _amount);
}

const jointVendorWithdraw = (_vendorid) => {
  return $web3js.commonSend(getIfilJointContract().methods.vendorWithdraw, _vendorid);
}

const jointVendorWithdrawEstimateGas = (_vendorid) => {
  return $web3js.commonEstimateGas(getIfilJointContract().methods.vendorWithdraw, _vendorid);
}

const getJointPoolInfo = (_vendorid) => {
  return $web3js.commonCallAndHandler(getIfilJointContract().methods.getMiningPoolInfo, handlePoolInfo, _vendorid);
}

const handlePoolInfo = (res) => {
  let totalAmount = Vue.prototype.$numberUtils.movePointLeft(
    res[0],
    18
  );
  let liquidAmount = Vue.prototype.$numberUtils.movePointLeft(
    res[1],
    18
  );
  let exchangeAmount = Vue.prototype.$numberUtils.movePointLeft(
    res[2],
    18
  );
  let tokenPrice = Vue.prototype.$numberUtils.movePointLeft(
    res[3],
    4
  );
  return {
    totalAmount,
    liquidAmount,
    exchangeAmount,
    tokenPrice,
  }
}

const getJointPoolGlobalInfo = (_vendorid) => {
  return $web3js.commonCallAndHandler(getIfilJointContract().methods.getGlobalInfo, handlePoolGlobalInfo, _vendorid);
}

const handlePoolGlobalInfo = (res) => {
  let index = 0;
  let afil = res[index++]; 
  let dtoken = res[index++]; 
  let userDeposits = Vue.prototype.$numberUtils.movePointLeft(
    res[index++],
    18
  );
  let userGains = Vue.prototype.$numberUtils.movePointLeft(
    res[index++],
    18
  );
  let vendorDeposits = Vue.prototype.$numberUtils.movePointLeft(
    res[index++],
    18
  );
  let vendorGains = Vue.prototype.$numberUtils.movePointLeft(
    res[index++],
    18
  );
  let vendorReleases = Vue.prototype.$numberUtils.movePointLeft(
    res[index++],
    18
  );
  let state = res[index++];
  let active = res[index++];
  return {
    afil,
    dtoken,
    userDeposits,
    userGains,
    vendorDeposits,
    vendorGains,
    vendorReleases,
    state,
    active,
  }
}

export default {
  getIfilJointAddress,
  getIfilJointContract,
  jointUserDeposit,
  jointUserDepositEstimateGas,
  jointUserRedeem,
  jointUserRedeemEstimateGas,
  jointVendorWithdraw,
  jointVendorWithdrawEstimateGas,
  getJointPoolInfo,
  getJointPoolGlobalInfo,
}